package com.example.demo.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

/**
 * @author liguang03
 * @date 2024/2/2
 */
public class CommonUtil {
    private static final Logger logger = LoggerFactory.getLogger(CommonUtil.class);

    public static boolean isNullId(Number id){
        return id == null || id.longValue() <= 0L;
    }

    public static boolean isNotNullId(Number id){
        return !isNullId(id);
    }

    public static <T> boolean isNullArray(T[] array){
        return array == null || array.length == 0;
    }

    public static <T> boolean isNotNullArray(T[] array){
        return !isNullArray(array);
    }


    /**
     * 集合转换，将形如1,2,3,4 字符串 转换为 集合
     *
     * @param str
     * @param convertorFun
     * @return
     */
    public static <R> List<R> collectionConvertor(String str, Function<String, R> convertorFun) {
        if (str != null && !str.isEmpty()) {
            List<R> list = new ArrayList<>();
            String[] strArray = str.split(",");
            for (String s : strArray) {
                R result = convertorFun.apply(s);
                if (result != null) {
                    list.add(result);
                }
            }
            return list;
        }
        return Collections.emptyList();
    }

    /**
     * 将形如 1,2,3,4 字符串 转为 a,b,c,d 字符串
     *
     * @param str
     * @param convertorFun
     * @return
     */
    public static String stringCollectionConvertor(String str, Function<String, String> convertorFun) {
        List<String> convertorResultList = collectionConvertor(str, convertorFun);
        return StringUtils.join(convertorResultList, ",");
    }



    /**
     * 集合内所有元素是否满足比较方法，比较方法由外部传入（可比较集合元素对象中的具体某个属性）<br/>
     *
     * @param source
     * @param containFun
     * @return 如果有一个不满足比较条件，则整个返回false，否则返回true
     */
    public static <T> boolean collectionsContainAllItems(List<T> source, Function<T, Boolean> containFun) {
        if (source != null && containFun != null) {
            for (T item : source) {
                if (!containFun.apply(item)) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 集合内任意元素是否比较方法，比较方法由外部传入（可比较集合元素对象中的具体某个属性）<br/>
     *
     * @param source
     * @param containFun
     * @return 如果有一个满足比较条件，则整个返回true，否则返回false
     */
    public static <T> boolean collectionsContainAnyItem(List<T> source, Function<T, Boolean> containFun) {
        if (source != null && containFun != null) {
            for (T item : source) {
                if (containFun.apply(item)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 将Map转为HTTP参数
     *
     * @param map
     * @return
     */
    public static String convertMapToHttpParam(Map<String, Object> map) {
        if(map == null || map.isEmpty()) {
            return "";
        }

        List<String> list = new ArrayList<>();
        Set<Map.Entry<String, Object>> entries = map.entrySet();
        Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
        while(iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            String key = entry.getKey();
            Object value = entry.getValue() == null ? "" : entry.getValue();
            String param = key + "=" + value.toString();
            list.add(param);
        }
        return StringUtils.join(list, "&");
    }

}
